/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.eduardcapell.lopodder.io.mp3;

import com.eduardcapell.lopodder.beans.FeedElement;
import java.io.File;
import javazoom.jlgui.basicplayer.BasicController;
import javazoom.jlgui.basicplayer.BasicPlayer;
import javazoom.jlgui.basicplayer.BasicPlayerException;
import com.eduardcapell.lopodder.gui.PlayerGUI;


/**
 *
 * @author edu
 */
public class SongPlayer implements Runnable {
    private FeedElement feedElement = null;
    private boolean playing = false;
    private BasicPlayer player = null;
    private BasicController control = null;

    public SongPlayer(FeedElement feedElement, PlayerGUI gui) throws Exception {
        this.feedElement = feedElement;
        player = new BasicPlayer();
        control = (BasicController) player;
        PlayerListener pl = new PlayerListener(gui);
        player.addBasicPlayerListener(pl);
        control.open(new File(feedElement.getFileName()));
    }


    public boolean isPlaying() {
        return playing;
    }


    public void pause() {
        if (player.getStatus() == BasicPlayer.PAUSED) {
            System.out.println("Resuming...");
            try {
                control.resume();
            } catch (BasicPlayerException ex) {
                ex.printStackTrace();
            }
        } else {
            try {
                control.pause();
            } catch (BasicPlayerException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void stop() {
        try {
            control.stop();
        } catch (BasicPlayerException ex) {
            ex.printStackTrace();
        }
    }

    public void run() {
        try {
            control.play();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
